package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.GoodsTypeService;
import com.atguigu.jxc.service.LogService;
import com.google.gson.annotations.JsonAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {

    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Autowired
    private LogService logService;

    @Override
    public String loadGoodsType() {
        List<Map<String,Object>> allList = new ArrayList<>();
        //所有类别
        GoodsType goodsType = goodsTypeDao.getOneGoodsType();
        Map<String,Object> OneMap = new HashMap<>();
        OneMap.put("id",goodsType.getGoodsTypeId());
        OneMap.put("text",goodsType.getGoodsTypeName());
        OneMap.put("state",goodsType.getGoodsTypeState()==1?"closed":"open");
        OneMap.put("iconCls","goods-type");
        Map<String,Object> lmap = new HashMap<>();
        lmap.put("state",goodsType.getGoodsTypeState());
        OneMap.put("attributes",lmap);

        //获取分类的类别
        List<GoodsType> goodsTypeList = goodsTypeDao.getGoodsTypeChildren(goodsType.getGoodsTypeId());
        List<Map<String,Object>> OneMapList = new ArrayList<>();
        //遍历分类类别存在OneMapList中
        for (GoodsType type : goodsTypeList) {
            Map<String,Object> TwoMap = new HashMap<>();
            TwoMap.put("id",type.getGoodsTypeId());
            TwoMap.put("text",type.getGoodsTypeName());
            TwoMap.put("state",type.getGoodsTypeState()==1?"closed":"open");
            TwoMap.put("iconCls","goods-type");
            Map<String,Object> llmap = new HashMap<>();
            llmap.put("state",type.getGoodsTypeState());
            TwoMap.put("attributes",llmap);

            //具体分类
            List<GoodsType> DetailGoodsTypeList = goodsTypeDao.getGoodsTypeChildren(type.getGoodsTypeId());
            List<Map<String,Object>> ThreeMapList = new ArrayList<>();
            if(DetailGoodsTypeList.size() != 0){
                for (GoodsType detailGoodsType : DetailGoodsTypeList) {
                    Map<String,Object> ThreeMap = new HashMap<>();
                    ThreeMap.put("id",detailGoodsType.getGoodsTypeId());
                    ThreeMap.put("text",detailGoodsType.getGoodsTypeName());
                    ThreeMap.put("state",detailGoodsType.getGoodsTypeState()==1?"closed":"open");
                    ThreeMap.put("iconCls","goods-type");
                    Map<String,Object> lllmap = new HashMap<>();
                    lllmap.put("state",detailGoodsType.getGoodsTypeState());
                    ThreeMap.put("attributes",lllmap);
                    ThreeMapList.add(ThreeMap);
                }
            }
                TwoMap.put("children",ThreeMapList);

            OneMapList.add(TwoMap);
        }

        OneMap.put("children",OneMapList);

        allList.add(OneMap);
       return  JSON.toJSONString(allList);


    }

    //添加分类 pid = 1为总分类
    @Override
    public ServiceVO save(String goodsTypeName, Integer pId) {
        //查询是否添加过 添加
        GoodsType goodsType = goodsTypeDao.goodsTypeIsExist(goodsTypeName);
        if (goodsType != null){
            //存在 提醒
            return new ServiceVO(ErrorCode.ACCOUNT_EXIST_CODE, ErrorCode.ACCOUNT_EXIST_MESS);
        }
        goodsTypeDao.save(goodsTypeName,pId);

        logService.save(new Log(Log.INSERT_ACTION,"新增类别:"+goodsTypeName));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(Integer goodsTypeId) {
        goodsTypeDao.delete(goodsTypeId);
        logService.save(new Log(Log.DELETE_ACTION,"删除类别成功"));

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}
